<template>
  <!-- 逻辑：在这个页面获取数据，通过props传参的方式给各个组件传参 -->
  <div class="report">
    <tableTool>
      <div slot="export">
        <el-button type="primary" @click="excelExport">导出</el-button>
      </div>
      <div class="tool-form" slot="form">
        <div class="input-group">
          <span class="lable">设备名称：</span>
          <el-input placeholder="请输入" v-model="filterEquipmentName" />
        </div>
        <div class="input-group">
          <span class="lable">设备类别：</span>
          <el-input placeholder="请输入" v-model="filterCategoryName" />
        </div>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="success" @click="onReset">重置</el-button>
      </div>
    </tableTool>
    <customTable :list="list" :combined="combined">
      <template v-slot:title>
        <tr>
          <th colspan="11">{{ orgName }}</th>
        </tr>
        <tr>
          <th rowspan="2">设备编号</th>
          <th rowspan="2">设备名称</th>
          <th rowspan="2">规格型号</th>
          <th rowspan="2">数量</th>
          <th rowspan="2">进场时间</th>
          <th rowspan="2">退场时间</th>
          <th rowspan="2">使用地点</th>
          <th rowspan="2">设备来源</th>
          <th rowspan="2">出租/出卖单位</th>
          <th colspan="2">租赁费/设备款支付情况</th>
        </tr>
        <tr>
          <th>已付</th>
          <th>未付</th>
        </tr>
      </template>
      <template #body="{ list }">
        <tr v-for="item in list" :key="item.id">
          <td>
            <span>{{ item.equipmentNumber }}</span>
          </td>
          <td>{{ item.equipmentName }}</td>
          <td>{{ item.equipmentStandard }}</td>
          <td>{{ item.quantity }}</td>
          <td>{{ item.enterDate }}</td>
          <td>{{ item.exitDate }}</td>
          <td>{{ item.useSite }}</td>
          <td>{{ item.equipmentSource }}</td>
          <td>{{ item.leaseSaleUnit }}</td>
          <td>{{ item.paid }}</td>
          <td>{{ item.unpaid }}</td>
        </tr>
      </template>
      <template #empty>
        <tr>
          <td colspan="11" class="empty">暂无数据</td>
        </tr>
      </template>
      <template #footer="{ combined }">
        <tr>
          <td>合计</td>
          <td>{{ combined.equipmentName }}</td>
          <td>{{ combined.equipmentStandard }}</td>
          <td>{{ combined.quantity }}</td>
          <td>{{ combined.enterDate }}</td>
          <td>{{ combined.exitDate }}</td>
          <td>{{ combined.useSite }}</td>
          <td>{{ combined.equipmentSource }}</td>
          <td>{{ combined.leaseSaleUnit }}</td>
          <td>{{ combined.paid }}</td>
          <td>{{ combined.unpaid }}</td>
        </tr>
      </template>
    </customTable>
    <pagination @onCurrentChange="handleCurrentChange" :total="page.total"></pagination>
  </div>
</template>
<script>
import tableTool from '../components/tableTool.vue'
import customTable from '../components/customTable.vue'
import pagination from '../components/pagination.vue'
import { combinedFn, formatTime } from '@/pages/comprehensiveReport/equipmentManagement/mixin'
import request from '@/api/comprehensiveReport/equipmentManagement.js'
export default {
  name: 'c',
  components: { tableTool, customTable, pagination },
  data () {
    return {
      list: [],
      orgName: '',
      page: {
        total: 0,
        currentPage: 1,
        limit: 10,
      },
      combined: {},
      headers: {
        // 定义对应关系- 把英文的key转化成中文的key所对应的值
        设备编号: 'equipmentNumber',
        设备名称: 'equipmentName',
        规格型号: 'equipmentStandard',
        数量: 'quantity',
        进场时间: 'enterDate',
        退场时间: 'exitDate',
        使用地点: 'useSite',
        设备来源: 'equipmentSource',
        '出租/出卖单位': 'leaseSaleUnit',
        已付: 'paid',
        未付: 'unpaid',
      },
      filterEquipmentName: '',
      filterCategoryName: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    onSearch () {
      this.getList()
    },
    onReset () {
      this.filterEquipmentName = ''
      this.filterCategoryName = ''
      this.getList()
    },
    async getList () {
      const orgId = this.$route.query.orgId || localStorage.orgId
      this.orgName = this.$route.query.orgName || this.$store.state.userInfo.orgInfo.orgName
      const data = await request.getEquipmentExternalDetails({
        page: this.page.currentPage,
        limit: this.page.limit,
        orgId,
        filterEquipmentName: this.filterEquipmentName,
        filterCategoryName: this.filterCategoryName
      })
      console.log('发送请求，获取数据', data)
      if (data.list.length == 0) {
        this.list = []
        this.combined = {}
        this.page.total = 0
        return;
      }
      this.page.total = data.totalCount
      const { list, combined } = combinedFn(Object.values(this.headers).slice(1), data.list, ['quantity', 'paid', 'unpaid'])
      this.list = formatTime(['enterDate', 'exitDate'], list)
      this.combined = combined
    },
    handleCurrentChange (currentPage) {
      this.page.currentPage = currentPage
      this.getList()
    },
    excelExport () {
      const multiHeader = [
        [this.orgName, '', '', '', '', '', '', '', '', '', ''],
        [
          '设备编号',
          '设备名称',
          '规格型号',
          '数量',
          '进场时间',
          '退场时间',
          '使用地点',
          '设备来源',
          '出租/出卖单位',
          '租赁费/设备款支付情况',
          '',
        ],
      ] // 它是一个二维数组
      const merges = ['A1:K1', 'J2:K2', 'A2:A3', 'B2:B3', 'C2:C3', 'D2:D3', 'E2:E3', 'F2:F3', 'G2:G3', 'H2:H3', 'I2:I3']
      // 懒加载 script-loader
      import('@/utils/Export2Excel.js').then(async (excel) => {
        const orgId = this.$route.query.orgId || localStorage.orgId
        // 获取所有的数据
        const res = await request.getEquipmentExternalDetails({
          page: 1,
          limit: this.page.total,
          orgId,
        })
        const list = formatTime(['mobilizationTime', 'checkoutTime'], res.list)
        // list就是所有的数据，list [{},{},{}] 转换为 => [[],[],[],[]]
        const data = this.formatJson(this.headers, list) // 转化数据表结构
        data.push(['合计', ...Object.values(this.combined)])
        excel.export_json_to_excel({
          header: Object.keys(this.headers), // Object.keys将一个对象的key组成一个数组
          data,
          filename: '设备管理-工程项目外协设备统计表-' + this.orgName, // 非必填
          autoWidth: true, // 非必填
          bookType: 'xlsx', // 非必填
          multiHeader, // 复杂表头
          merges, // 合并选项
        })
      })
    },
    formatJson (headers, list) {
      return list.map((item) => {
        return Object.values(headers).map((value) => {
          return item[value]
        })
      })
    },
  },
}
</script>
<style lang="scss">

</style>
